/**
 * Created by 35894 on 2017/9/5.
 */
define([
    "app"
],function (app) {
    app.controller("selectSeatController", [
        "$scope",
        "$stateParams",
        "selectSeatDataService",
        "utilService",
        "$state",
        function ($scope, $stateParams, selectSeatDataService, utilService,$state) {
            selectSeatDataService
                .getAllTeamWithoutSeat()
                .then(
                    function success(resp) {

                        $scope.teams = selectSeatDataService.teams = resp.data;

                        $scope.hoveredSeat = selectSeatDataService.hoveredSeat;
                        $scope.currentTeam = selectSeatDataService.currentTeam;
                        $scope.currentRoomNo = selectSeatDataService.currentRoomNo;

                        // 选择团队
                        $scope.selectTeam = function (team) {
                            selectSeatDataService.currentTeam.data = team;

                            // 为所有成员添加一个order属性，代表选择的顺序
                            // 切换团队后同时也会清空已经选择的顺序
                            selectSeatDataService.currentTeam.data.members.forEach(function (el, i, arr) {
                                el.order = null;
                            });
                        };

                        // 被选中的成员
                        var memberSelected = [];

                        $scope.isObjectEmpty = utilService.isObjectEmpty;

                        // 选择成员
                        $scope.selectMember = function (member) {
                            if (member.order) {
                                memberSelected.splice(memberSelected.lastIndexOf(member), 1);
                                member.order = null;
                            } else {
                                memberSelected.push(member);
                            }

                            memberSelected.forEach(function (el, i, arr) {
                                el.order = i + 1;
                            });
                        };

                        // 提交数据
                        $scope.submit = function () {
                            var seatSelected = selectSeatDataService.seatSelected.data;
                            if (seatSelected.length === 0) {
                                alert("未选择座位!");
                                return;
                            }
                            if (memberSelected.length === 0) {
                                alert("未选择成员!");
                                return;
                            }
                            if (seatSelected.length !== memberSelected.length) {
                                alert("成员数和座位数不匹配!");
                                return;
                            }

                            console.log(seatSelected);
                            console.log(memberSelected);

                            if (!confirm("确定要提交吗?")) {
                                return
                            }

                            var dataSubmit = [];

                            seatSelected.forEach(function (e, i, arr) {
                                dataSubmit.push({
                                    roomNo: e.roomNo,
                                    seatNo: e.seatNo,
                                    studentId: memberSelected[i].studentId
                                });
                            });

                            selectSeatDataService
                                .submit(dataSubmit)
                                .then(
                                    function success(resp) {
                                        alert("提交成功!");
                                        selectSeatDataService.reInitData();
                                        $state.go($state.current, {}, {reload: true});
                                    },
                                    function error() {
                                        alert("似乎出了点问题!!!∑(ﾟДﾟノ)ノ");
                                        selectSeatDataService.reInitData();
                                        $state.go($state.current, {}, {reload: true});
                                    });
                        }
                    },
                    function error(resp) {

                    }
                )
        }
    ]);
});